import gevent
from gevent import monkey
monkey.patch_all()

from bottle import static_file,route, run,template,get, post, request ,response
import os,re,time,random,threading
import sqlite3
import re
import app.userhandler as uhd

# # # # # # # # # # # # # # # # # # # examples # # # # # # # # # # # # # # # # # # # # # # #





#4.  Multi-threading
def shellThread(cmd):
    time.sleep(0.1) #must add this sleep under the frame of gevent
    os.system(cmd)  #execute a cmd or shell command

# t1=threading.Thread(target=shellThread,args=("sh ./shell/gitClone.sh "+gitLib_Name+" "+ libre_git+" "+mcu_firmware,))
# t1.start()

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@route('/')
def func():
    return static_file("main.html", root='./static/html/',download=False)

@get('/download/<filename:path>')
def send_static(filename):
    return static_file(filename, root='./',download=True)

@get('/show/<filename:path>')
def send_static(filename):
    print(filename)
    return static_file(filename, root='./',download=False)

@route('/restricted')
def restricted_area():
    username = request.get_cookie("account", secret='some-secret-key')
    if username:
        return template("base",text="Hello %s. Welcome back."%username,lineNum=2)
    else:
        return template("base",text="You are not logged in. Access denied.",lineNum=2)


@post('/submit/<context:path>')
def func(context):
    try:
        route2=re.match("(.*?)/",name).group(1)   #解析二级路由
    except:
        route2=context

    if(route2=="login"):
        return uhd.loginDeal()
    else:
        return template("base",text="Nothing",lineNum=1)

run(host='0.0.0.0', port=80, debug=True,server='gevent')